package com.bruce.starts.demo.thread.demo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class TeaDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TeaDemo teaDemo = new TeaDemo();
        teaDemo.makeTea();
    }
     public void makeTea() throws ExecutionException, InterruptedException {
         FutureTask<String> f2 = new FutureTask<>(new Task2());
         FutureTask<String> f1 = new FutureTask<>(new Task1(f2));
         Thread t1 = new Thread(f1);
         t1.start();
         Thread t2 = new Thread(f2);
         t2.start();
         System.out.println(f1.get());
     }
}

class Task1 implements Callable<String>{
    FutureTask<String> task;

    public Task1(FutureTask<String> task) {
        this.task = task;
    }

    @Override
    public String call() throws Exception {
        System.out.println("T1: 洗水壶...");
        TimeUnit.SECONDS.sleep(1);

        System.out.println("T1: 烧开水...");
        TimeUnit.SECONDS.sleep(15);
        // 获取 T2 线程的茶叶
        String tf = task.get();
        System.out.println("T1: 拿到茶叶:"+tf);

        System.out.println("T1: 泡茶...");
        return " 上茶:" + tf;
    }
}

class Task2 implements Callable<String>{
    @Override
    public String call() throws Exception {
        System.out.println("T2: 洗茶壶...");
        TimeUnit.SECONDS.sleep(1);

        System.out.println("T2: 洗茶杯...");
        TimeUnit.SECONDS.sleep(2);

        System.out.println("T2: 拿茶叶...");
        TimeUnit.SECONDS.sleep(1);
        return "龙井";
    }
}
